package com.fauks.ssm.mybatis;

import com.fauks.ssm.mybatis.bean.MProxy;
import com.fauks.ssm.mybatis.mapper.ProxyMapper;
import com.fauks.ssm.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试Mapper动态代理
 */
public class _02MyBatisTestProxy {

    private SqlSession sqlSession;
    private ProxyMapper iProxyDao;

    @Before
    public void initDao() {
        sqlSession = MyBatisUtils.getSqlSession();
        iProxyDao = sqlSession.getMapper(ProxyMapper.class);
    }

    @After
    public void closeSession() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    @Test
    public void testInsert() {
        iProxyDao.insertProxyBean(new MProxy("fauks", 12));
        //提交事务
        sqlSession.commit();
    }

    @Test
    public void testSelectAllProxy() {
        List<MProxy> mProxies = iProxyDao.selectAllProxy();
        for (MProxy mProxy : mProxies) {
            System.out.println(mProxy.getName());
        }
    }

    @Test
    public void testSelectAllProxyByMap() {
        Map<String, Object> params = new HashMap<>();
        params.put("name", "a");
        params.put("age", 10);
        MProxy proxy = new MProxy();
        proxy.setScore(6.0f);
        params.put("score", proxy);
        List<MProxy> mProxies = iProxyDao.selectProxyByNameAndAgeMap(params);
        for (MProxy mProxy : mProxies) {
            System.out.println(mProxy.getName());
        }
    }

    @Test
    public void testSelectAllProxyByIndex() {
        List<MProxy> mProxies = iProxyDao.selectProxyByIndex("a", 10);
        for (MProxy mProxy : mProxies) {
            System.out.println(mProxy.getName());
        }
    }

    @Test
    public void testSelectByAge() {
        MProxy proxy = iProxyDao.selectProxyByAge(33);
        System.out.println(proxy.getName());
    }
}
